package configuration

import (
	"fmt"
	"time"
	"v8s/logger"

	server "v8s/http-server"

	"github.com/caarlos0/env/v6"
)

type (
	Properties struct {
		Log logger.Properties `envPrefix:"LOG_"`

		Feature FeatureProperties `envPrefix:"FEATURE_"`

		Mongo            MongoProperties             `envPrefix:"MONGO_"`
		HttpServer       server.HttpServerProperties `envPrefix:"HTTP_"`
		ManagementServer server.HttpServerProperties `envPrefix:"MANAGEMENT_"`
		Task             TaskProperties              `envPrefix:"TASK_"`

		TaskMetricsController     SchedulerProperties `envPrefix:"TASK_METRICS_"`
		SynchronizationController SchedulerProperties `envPrefix:"SYNCHRONIZATION_"`
		LoggingController         SchedulerProperties `envPrefix:"LOGGING_"`

		Generator GeneratorProperties `envPrefix:"GENERATOR_"`
		Logging   LoggingProperties   `envPrefix:"LOGGING_"`
	}

	GeneratorProperties struct {
		Container            ContainerProperties      `envPrefix:"CONTAINER_"`
		ImagePullSecretFiles []string                 `env:"IMAGE_PULL_SECRET_FILES"`
		Quota                NamespaceQuotaProperties `envPrefix:"NAMESPACE_QUOTA_"`
		Volumes              VolumesProperties        `envPrefix:"VOLUMES_"`
	}

	ContainerProperties struct {
		RuntimeClass string `env:"RUNTIME_CLASS"`
	}

	NamespaceQuotaProperties struct {
		CPULimit              string `env:"CPU_LIMIT" envDefault:"1000m"`
		RAMLimit              string `env:"RAM_LIMIT" envDefault:"1024Mi"`
		EphemeralStorageLimit string `env:"EPHEMERAL_STORAGE_LIMIT" envDefault:"20Gi"`
	}

	LoggingProperties struct {
		Host          string `env:"HOST"`
		User          string `env:"USER"`
		Password      string `env:"PASSWORD"`
		DashboardHost string `env:"DASHBOARD_HOST"`
	}

	FeatureProperties struct {
		LoggingEnabled bool `env:"LOGGING" envDefault:"false"`
	}

	VolumesProperties struct {
		DefaultStorageClassName string `env:"DEFAULT_STORAGE_CLASS_NAME,notEmpty"`
	}

	TaskProperties struct {
		ProvisioningDeadline time.Duration `env:"PROVISIONING_DEADLINE" envDefault:"5m"`
	}

	MongoProperties struct {
		ConnectionURI string `env:"CONNECTION_URI"`
		Database      string `env:"DATABASE,notEmpty"`
	}

	SchedulerProperties struct {
		Timeout              time.Duration `env:"TIMEOUT" envDefault:"1m"`
		TaskExecutionTimeout time.Duration `env:"TASK_EXECUTION_TIMEOUT" envDefault:"30m"`
	}
)

func ReadProperties() *Properties {
	config := &Properties{}
	if err := env.Parse(config); err != nil {
		panic(fmt.Errorf("read config error: %w", err))
	}

	fmt.Printf("config: %+v", config)
	return config
}
